package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;


    //@RequiredPermission(name = "部门列表",expression = "permission:list")
    @RequestMapping("/list")
    public String listAll(Model model,QueryObject qo){
        PageInfo<Permission> pageResult = permissionService.query(qo);
        model.addAttribute("pageResult",pageResult);
        //因为配了 前缀和后缀
        return "permission/list";
    }

    //处理删除请求
    @RequestMapping("/delete")
    @ResponseBody
    //@RequiredPermission(name = "部门删除",expression = "permission:delete")
    public JsonResult delete(Long id){
        try {
            //int a = 1/0;
            if (id != null){
                permissionService.delete(id);
            }
            return new JsonResult(true,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
    }

    //@RequiredPermission(name = "权限加载",expression = "permission:reload")
    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload(){
        try {
            permissionService.reload();
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"加载失败");
        }
    }
}
